# -*- coding: utf-8 -*-

import os

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.escape

from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)

settings = {
    "static_path":os.path.join(os.path.dirname(__file__), "_static"),
    "template_path":os.path.join(os.path.dirname(__file__), "_templates"),
    #"autoescape":"xhtml_escape"
}

def getMySQLConn():
    config=ConfigParser.RawConfigParser(allow_no_value=True)
    with open('C:/mysql.ini') as f:
        config.readfp(f)
    db=MySQLdb.connect(host=config.get('mysqld','host'),
        user=config.get('mysqld','user'),passwd=config.get('mysqld','pw'),
        db="zhihu",charset=config.get('mysqld','charset'))
    return db

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", MainHandler),
            (r"/teamwork/(.*)",TeamworkHandler)
        ]
        tornado.web.Application.__init__(self, handlers, **settings)

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        #self.render("index.html")
        self.write("<h1>HelloWorld</h1>")

class TeamworkHandler(tornado.web.RequestHandler):
    def get(self,section):
        if section=='index':
            self.render('index.html')
        elif section=='home':
            self.render('home.html')
        elif section=='about':
            self.render('about.html')


def main():
    tornado.options.parse_command_line()
    app=Application()
    #http_server = tornado.httpserver.HTTPServer(app)
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    main()
